package com.yykj.tencent.im.server.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yykj.tencent.im.client.constant.TagProfile;
import com.yykj.tencent.im.server.bean.request.*;
import com.yykj.tencent.im.server.bean.response.*;
import com.yykj.tencent.im.server.core.TimUtil;
import com.yykj.tencent.im.server.exception.TimException;
import com.yykj.tencent.im.server.service.AccountService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    /**
     * 版本
     */
    private final String Version = "v4";

    /**
     * 帐号管理服务名
     */
    private static final String SERVICE_NAME = "im_open_login_svc";
    private static final String SERVICE_NAME_OPEN_IM = "openim";

    /**
     * 帐号管理相关命令字
     */
    private static final String ACCOUNT_IMPORT_COMMAND = "account_import";
    private static final String MULTI_ACCOUNT_IMPORT_COMMAND = "multiaccount_import";
    private static final String ACCOUNT_DELETE_COMMAND = "account_delete";
    private static final String ACCOUNT_CHECK_COMMAND = "account_check";
    private static final String KICK_COMMAND = "kick";
    private static final String QUERY_ONLINE_STATUS_COMMAND = "query_online_status";

    /**
     * 请求接口
     */
    private final String api_url = "%s/%s/%s";



    @Override
    public String getUserSig(String identifier) {
        return TimUtil.getUserSig();
    }

    /**
     * 导入单个帐号
     */
    @SneakyThrows
    @Override
    public AccountImportResult accountImport(AccountImportRequest accountImportRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME, ACCOUNT_IMPORT_COMMAND);
        return TimUtil.post(api, accountImportRequest, AccountImportResult.class);
    }

    /**
     * 导入多个帐号
     */
    @SneakyThrows
    @Override
    public MultiAccountImportResult multiAccountImport(MultiAccountImportRequest multiAccountImportRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME, MULTI_ACCOUNT_IMPORT_COMMAND);
        return TimUtil.post(api, multiAccountImportRequest, MultiAccountImportResult.class);
    }

    /**
     * 删除帐号
     */
    @SneakyThrows
    @Override
    public AccountDeleteResult accountDelete(List<String> userIds) {
        if (userIds == null) {
            throw new TimException("账号不能为空");
        }
        List<AccountDeleteItem> items = new ArrayList<>();
        for (String s : userIds) {
            items.add(new AccountDeleteItem(s));
        }
        AccountDeleteRequest accountDeleteRequest = AccountDeleteRequest.builder()
                .deleteItemList(items).build();
        String api = String.format(api_url, Version, SERVICE_NAME, ACCOUNT_DELETE_COMMAND);
        return TimUtil.post(api, accountDeleteRequest, AccountDeleteResult.class);
    }

    /**
     * 查询帐号
     */
    @SneakyThrows
    @Override
    public AccountCheckResult accountCheck(List<String> userIds) {
        if (userIds == null) {
            throw new TimException("账号不能为空");
        }
        List<AccountCheckItem> items = new ArrayList<>();
        for (String s : userIds) {
            items.add(new AccountCheckItem(s));
        }
        AccountCheckRequest accountCheckRequest = AccountCheckRequest.builder()
                .checkItemList(items)
                .build();
        String api = String.format(api_url, Version, SERVICE_NAME, ACCOUNT_CHECK_COMMAND);
        return TimUtil.post(api, accountCheckRequest, AccountCheckResult.class);
    }

    /**
     * 失效帐号登录状态
     */
    @SneakyThrows
    @Override
    public KickResult kick(String userId) {
        if (userId.isEmpty()) {
            throw new TimException("账号不能为空");
        }
        KickRequest kickRequest = KickRequest.builder()
                .userId(userId)
                .build();
        String api = String.format(api_url, Version, SERVICE_NAME, KICK_COMMAND);
        return TimUtil.post(api, kickRequest, KickResult.class);
    }

    /**
     * 查询帐号在线状态
     */
    @SneakyThrows
    @Override
    public QueryOnlineStatusResult queryOnlineStatus(QueryOnlineStatusRequest queryOnlineStatusRequest) {
        String api = String.format(api_url, Version, SERVICE_NAME, QUERY_ONLINE_STATUS_COMMAND);
        return TimUtil.post(api, queryOnlineStatusRequest, QueryOnlineStatusResult.class);
    }

    @SneakyThrows
    @Override
    public PortraitGetResult portraitGet(List<String> imIds) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("To_Account", imIds);
        JSONArray tagList = new JSONArray();
        tagList.add(TagProfile.IM_NICK);
        tagList.add(TagProfile.IM_IMAGE);
        jsonObject.put("TagList", tagList);
        String api = String.format(api_url, Version, "profile", "portrait_get");
        PortraitGetResult portraitGetResult = TimUtil.post(api, jsonObject, PortraitGetResult.class);
        return portraitGetResult;
    }
}
